#!/usr/bin/env python  
# -*- coding: utf-8 -*-  
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
from smtplib import SMTP
import sys,re,urllib2,urllib,time,xlwt,os,json,codecs,csv,smtplib

type = sys.getfilesystemencoding() 
reload(sys)  
sys.setdefaultencoding('utf8')


class Youtube_play:
    #开始方法
    def __init__(self):
        self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        self.headers    = { 'User-Agent' : self.user_agent }
        self.channel_id = ''
        self.item_datas = []
        self.video_id = []
        self.channel_playlist = []
        self.viewCount = 0
        self.commentCount = 0

    def start(self,channel_id):
        self.channel_id = channel_id
        
        # 获取播放主题列表ID
        self.getPlayListsId(channel_id,'')

        # 获取主题的播放列表ID
        for item in self.item_datas:
            self.getPalyItemsId(item,'')

    def getPlayListsId(self,channel_id, pageToken):
        url             = 'https://www.googleapis.com/youtube/v3/playlists'
        part            = 'snippet,contentDetails'
        key             = 'AIzaSyBS8p-aiWmI6jeCFvRZTYKgO5AkqrAXmkA'
        channel_url     = url + '?channelId='+ channel_id +'&part='+ part +'&key='+ key +'&maxResults=50' + '&pageToken='+ pageToken
        request         = urllib2.Request(channel_url, headers = self.headers)
        response        = urllib2.urlopen(request).read()
        channel_msg     = json.loads(response)

        for item in channel_msg['items']:
            item_data = {
                'id':item['id'],
                'title':item['snippet']['localized']['title'],
                'itemCount' : item['contentDetails']['itemCount']
            }
            self.item_datas.append(item_data)

        try:
            if(channel_msg['nextPageToken']):
                print channel_msg['nextPageToken']
                self.getChannelData(channel_id,channel_msg['nextPageToken'])
        except:
            self.item_datas = self.item_datas[26:48]

            print u"获取列表ID一共有%s"%(len(self.item_datas))



    def getPalyItemsId(self,item_data,pageToken):
        url             = 'https://www.googleapis.com/youtube/v3/playlistItems'
        part            = 'snippet,contentDetails'
        key             = 'AIzaSyBS8p-aiWmI6jeCFvRZTYKgO5AkqrAXmkA'
        channel_url     = url + '?playlistId='+ item_data['id'] +'&part='+ part +'&key='+ key +'&maxResults=10' + '&pageToken='+ pageToken
        request         = urllib2.Request(channel_url, headers = self.headers)
        response        = urllib2.urlopen(request).read()
        channel_msg     = json.loads(response)

        self.viewCount = 0
        self.commentCount = 0
        self.video_id = []

        for item in channel_msg['items']:
            self.video_id.append(item['snippet']['resourceId']['videoId'])

        try:
            if(channel_msg['nextPageToken']):
                self.getPlayListsId(item_data['id'],channel_msg['nextPageToken'])
        except:
            print u"播放主题%s,获取影片ID一共有%s"%(item_data['title'],len(self.video_id))
            for video in self.video_id:
                self.getVideo(video)

            self.calculatorData(item_data)

    def getVideo(self,video_id):
        url             = 'https://www.googleapis.com/youtube/v3/videos'
        part            = 'statistics'
        key             = 'AIzaSyBS8p-aiWmI6jeCFvRZTYKgO5AkqrAXmkA'
        channel_url     = url + '?id='+ video_id +'&part='+ part +'&key='+ key +'&maxResults=50'
        request         = urllib2.Request(channel_url, headers = self.headers)
        response        = urllib2.urlopen(request).read()
        channel_msg     = json.loads(response)

        # print channel_url
        # print channel_msg['items'][0]['statistics']['viewCount']
        # print channel_msg['items'][0]['statistics']['commentCount']

        try:
            self.viewCount += int(channel_msg['items'][0]['statistics']['viewCount'])
            self.commentCount += int(channel_msg['items'][0]['statistics']['commentCount'])
        except:
            print u"私人影片无法捉取%s"%(video_id)

        # for item in channel_msg['items']:
        #     self.video_id.append(item['snippet']['resourceId']['videoId'])

        # try:
        #     if(channel_msg['nextPageToken']):
        #         self.getPlayListsId(item_data['id'],channel_msg['nextPageToken'])
        # except:
        #     self.getVideo(item_data)
        #     print u"获取项目列表ID一共有%s"%(len(self.video_id))

    def calculatorData(self,item_data):
        title  = item_data['title']
        itemCount =  item_data['itemCount']
        viewCount =  self.viewCount/int(item_data['itemCount'])
        commentCount =  self.commentCount/int(item_data['itemCount'])
        

        if os.path.exists('./play_list.csv') == False:
            with codecs.open('./play_list.csv', 'w', 'utf_8_sig') as f:
                f.write('GO车志,播放列表数量,平均瀏覽數,平均評論數\n')
                f.write('%s,%s,%s,%s'%(title,itemCount,viewCount,commentCount))
        else:
            with codecs.open('./play_list.csv', 'a', 'utf_8_sig') as f:
                f.write('\n%s,%s,%s,%s'%(title,itemCount,viewCount,commentCount))       
         

spider = Youtube_play()

channel_id_array = [
    'UC-MByr5LRnWmVN94Ija4XJg'
]
for i in channel_id_array:
    spider.start(i)

